﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Text;

namespace JLL.China.Infrastructure.Repositories.ProjectDirFloorPlan
{
    public class PrjDirFlrPlanRepository:SQLRepositoryBase<Model.ProjectDirFloorPlan, int>, IPrjDirFlrPlanRepository
    {
        public override Model.ProjectDirFloorPlan FindBy(int key)
        {
            throw new NotImplementedException();
        }

        public override IList<Model.ProjectDirFloorPlan> FindAll()
        {
            throw new NotImplementedException();
        }

        protected override void PersistNewItem(Model.ProjectDirFloorPlan item)
        {
            DataContext.sp_prjdirfloorplan_create(item.TypeModel, item.NoUnit, item.FloorPlanImg, item.ListingFID,
                                                  item.LanguageFID, item.ProjectDirFloorPlan_ID);
        }

        protected override void PersistUpdatedItem(Model.ProjectDirFloorPlan item)
        {
            DataContext.sp_prjdirfloorplan_update(item.TypeModel, item.NoUnit, item.FloorPlanImg, item.ListingFID,
                                                  item.LanguageFID, item.ProjectDirFloorPlan_ID);
        }

        protected override void PersistDeletedItem(Model.ProjectDirFloorPlan item)
        {
            DataContext.sp_prjdirfloorplan_delete(item.ProjectDirFloorPlan_ID);
        }
        public int Deleted(decimal? id)
        {
            return DataContext.sp_prjdirfloorplan_delete(id);
        }
        public  decimal  Create(Model.ProjectDirFloorPlan item)
        {
            return DataContext.sp_prjdirfloorplan_create(item.TypeModel, item.NoUnit, item.FloorPlanImg, item.ListingFID,
                                                  item.LanguageFID, item.ProjectDirFloorPlan_ID);
        }

        public decimal  Update(Model.ProjectDirFloorPlan item)
        {
            return DataContext.sp_prjdirfloorplan_update(item.TypeModel, item.NoUnit, item.FloorPlanImg, item.ListingFID,
                                                  item.LanguageFID, item.ProjectDirFloorPlan_ID);
        }
        public IList<Model.ProjectDirFloorPlan> FindByListingID_Language(decimal? listingID,decimal languageFID)
        {
            if(listingID != null)
            {
                var list = (from l in DataContext.ProjectDirFloorPlans
                            where l.ListingFID.Equals(listingID) && l.LanguageFID.Equals(languageFID)
                            orderby l.ProjectDirFloorPlan_ID descending 
                           select l).ToList();


                return list.Select(projectDirFloorPlan => PrjDirFlrPlanRefactory.FromLINQObject(projectDirFloorPlan)).ToList();
            }
            return null;
        }
        public Model.ProjectDirFloorPlan FindByID_Language(decimal? ID, decimal languageFID)
        {
            if (ID != null)
            {
                var list = (from l in DataContext.ProjectDirFloorPlans
                            where l.ProjectDirFloorPlan_ID.Equals(ID) && l.LanguageFID.Equals(languageFID)
                            select l).SingleOrDefault();
                
                return PrjDirFlrPlanRefactory.FromLINQObject(list);
            }
            return null;
        }
    }
}
